package com.sunyard.pcc.common.shiro.impl;

import com.sunyard.pcc.common.bo.SysUserCacheBO;
import com.sunyard.pcc.common.constant.ErrorCodeEnum;
import com.sunyard.pcc.common.exception.PccCommonException;
import com.sunyard.pcc.common.shiro.UserSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @Author: Hewei
 * @Description: 用户存储接口实现类，实现Session存储
 * @Date: Created in 15:40 2018/3/12
 */
@Slf4j
@Component(value = "sessionCache")
public class SessionUserSaveServiceImpl implements UserSaveService {

    //用户标识
    private static final String SESSION_USER = "session_user";


    @Override
    public SysUserCacheBO getCacheUser() throws PccCommonException {
        return (SysUserCacheBO) getSession().getAttribute(SESSION_USER);
    }

    @Override
    public void saveUser(SysUserCacheBO user) throws PccCommonException {
        Optional.ofNullable(user)
                .orElseThrow(() -> new PccCommonException(ErrorCodeEnum.PARAM_EMPTY));

        getSession().setAttribute(SESSION_USER, user);

    }

    @Override
    public void removeUser() throws PccCommonException {
        getSession().removeAttribute(SESSION_USER);
    }
}
